﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace LdtJudge.DataEngine.PersonnelAnalysis.PersonnelBase
{
    /// <summary>
    /// 多人员同时发言处理逻辑类
    /// </summary>
    public static class MultiSpokeManParse
    {
        #region 基础信息
        private static List<string> RoleKeyWord = new List<string>() { "被", "被告", "代","上诉人" }; //身份关键字

        private static Regex numberParser = new Regex("[0-9０-９一二三四五六七八九十壹贰叁肆伍陆柒捌玖拾]");//匹配数字九个人
        #endregion

        #region 多人员处理方法
        /// <summary>
        /// 多人员发言分解为多个人员的处理方法
        /// </summary>
        /// <param name="spokename">发言人姓名(如被一、二代   |被三代、被五)</param>
        /// <returns>人员分割结果</returns>
        public static List<string> Parse(string spokename)
        {
            Regex reg = new Regex(string.Join("|", RoleKeyWord.ToArray()));
            List<string> allspokeman = spokename.Split(new char[] { '、' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (spokename.StartsWith("第")) //第一、二、三被告
            {
                if (allspokeman.Last().EndsWith("被告") && allspokeman.All(a => numberParser.IsMatch(a)))
                {
                    for (int i = 0; i < allspokeman.Count; i++)
                    {
                        allspokeman[i] = "第" + numberParser.Match(allspokeman[i]).Value + "被告";
                    }
                }

                return allspokeman;
            }
            else
            {
                var matched = allspokeman.Select((n, index) => new { n = n, index = index }).Where(a => RoleKeyWord.Any(b => a.n.Contains(b))).ToList();//逐个匹配
                if (allspokeman.All(a => reg.IsMatch(a) && a.StartsWith("被"))) //每个顿号都是独立的人 或者 类似 被一、三代这种
                {
                    return allspokeman;
                }
                else if (allspokeman.Where(a => reg.IsMatch(a)).Count() == 2 && allspokeman.First().StartsWith("被") && allspokeman.Last().EndsWith("代"))
                {
                    for (int i = 0; i < allspokeman.Count; i++)
                    {
                        allspokeman[i] = "被" + numberParser.Match(allspokeman[i]).Value + "代";
                    }
                    return allspokeman;
                }
                else if (matched.Count == 1 && matched.First().index == 0 && matched.Where(a => a.index > 0).All(a => IsNumber(a.n)))//同类型的人  如 被告一、二  被一、二 被1、2、3
                {
                    string keyword = numberParser.Replace(matched.First().n, "");
                    for (int i = 1; i < allspokeman.Count; i++)
                    {
                        if (IsNumber(allspokeman[i]))
                        {
                            allspokeman[i] = keyword + allspokeman[i];
                        }
                    }
                    return allspokeman;
                }
                else
                {
                    return new List<string>() { spokename };
                }
            }
        }

        /// <summary>
        /// 判断此文字是否是数字
        /// </summary>
        /// <param name="text">文本</param>
        /// <returns>是否为数字</returns>
        public static bool IsNumber(string text)
        {
            return numberParser.IsMatch(text);
        }
        #endregion
        
    }
}
